/*
 * acm1334.c
 *
 *  Created on: 20 Mar 2010
 *      Author: xuxu
 */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int chartoint(char c)
{
	int ret;
	if (c > '9')
	{
		ret = 10 + c - 'A';
	}
	else
	{
		ret = c - '0';
	}
	return ret;
}

char inttochar(int n)
{
	char ret;
	if (n > 9)
	{
		ret = n - 10 + 'A';
	}
	else
	{
		ret = n + '0';
	}
	return ret;
}

int chang(char *num, int sour, int dest)
{
	char tmp[50], tmp1[51];
	int result;
	int len, i, j;
	strcpy(tmp, num);
	len = strlen(tmp);
	result = 0;
	for (i = 0; i < len; i++)
	{
		result = result * sour + chartoint(tmp[i]);
	}
	i = 0;
	while (result)
	{
		tmp1[i++] = inttochar(result % dest);
		result /= dest;
	}
	len = i;
	for (j = 0; j < len; j++)
	{
		num[j] = tmp1[--i];
	}
	num[len] = '\0';
	return len;
}

int main()
{
	char cnum[50];
	int sour, dest;
	int changelen;
	while (scanf("%s%d%d", cnum, &sour, &dest) != EOF)
	{
		changelen = chang(cnum, sour, dest);
		if (changelen > 7)
		{
			printf("  ERROR\n");
		}
		else
		{
			printf("%7s\n", cnum);
		}
	}
	return 0;
}
